﻿/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2016/5/10
 * Time: 9:18
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;

namespace SharpErp.Data.Util
{

	/// <summary>
	/// Description of SerializerManager.
	/// </summary>
	public class SerializerManager
	{
		private class JSSerializer : ISerialize{
			
		#region ISerialize implementation
		public string Serialize<T>(T obj) where T : class
		{
			var ser = new JavaScriptSerializer();
			return ser.Serialize(obj);
		}
		public T Deserialize<T>(string str) where T : class
		{
			var ser = new JavaScriptSerializer();
			return ser.Deserialize<T>(str);
		}
		#endregion
		
		}
		
		private static SerializerManager _inst;
		
		private readonly Dictionary<String, ISerialize> mapName2Serializer = new Dictionary<string, ISerialize>();
		
		private const string KEY_JS = "js";
		
		public static SerializerManager GetInstance(){
			if(_inst == null) _inst = new SerializerManager();
			return _inst;
		}
		
		public static SerializerManager Instance(){
			return GetInstance();
		}
		
		public ISerialize GetJSSerializer(){
			if(mapName2Serializer.ContainsKey(KEY_JS)) return mapName2Serializer[KEY_JS];
			else mapName2Serializer[KEY_JS] = new JSSerializer();
			return mapName2Serializer[KEY_JS];
		}
	}
}
